<!-- 用户编辑弹窗 -->
<template>
  <ele-modal width="680px"
             :visible="visible"
             :append-to-body="true"
             :close-on-click-modal="true"
             custom-class="ele-dialog-form"
             :title="isUpdate ? '修改上级' : '修改上级'"
             @update:visible="updateVisible">
    <el-form ref="form"
             :model="form"
             :rules="rules"
             label-width="82px">
      <el-row :gutter="15">
        <el-col v-bind="styleResponsive ? { sm: 12 } : { span: 12 }">
          <el-form-item label="用户账号:"
                        prop="name">
            <div class="flex-between-start">
              <el-input clearable
                        :maxlength="20"
                        v-model="form.name"
                        placeholder="请输入用户手机号或者昵称" />
              <el-button type="primary"
                         :loading="loading"
                         @click="search">
                搜索
              </el-button>
            </div>
          </el-form-item>

        </el-col>

      </el-row>
      <div v-if="userInfo"
           style="overflow-y: auto; height: 160px">
        <div style="width: 70%;"
             class="flex-between margin_20px_0"
             v-for="(item,index) in userInfo"
             :key="index">

          <div class="flex-start-center">
            <img class="image"
                 :src="item.avatar"
                 alt="">
            <div class="flex-columns-center margin-left_20px">
              <span class="font-weight_bold margin-bottom_10px"
                    style="font-size: 15px;">昵称:{{item.nickname}}</span>
              <span style="color: rgb(153, 153, 153);font-size:12px;margin-bottom: 5px;">
                手机号:{{item.phone}}
              </span>

            </div>
          </div>
          <el-radio @input="radioSelect"
                    v-model="radio"
                    :label="index"><span></span></el-radio>
          <!-- <el-checkbox class="margin-left_20px"
                       @change="radioSelect"
                       v-model="radio"></el-checkbox> -->

        </div>
      </div>
    </el-form>
    <template v-slot:footer>
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button type="primary"
                 :loading="loading"
                 @click="save">
        保存
      </el-button>
    </template>
  </ele-modal>
</template>

<script>
import { phoneReg } from 'ele-admin';

import { checkExistence } from '@/api/system/user';
import {
  pageUsers,
  setTopUser

} from '@/api/user/user';

export default {
  components: {},
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  data () {

    const defaultForm = {
      id: null,
      name: '',
      real_name: '',
      roleIds: [],
      storeId: [],
      phone: '',
      password: '',
    };
    return {
      clickSearch: false,
      userInfo: null,
      defaultForm,
      radio: 0,
      // 表单数据
      form: { ...defaultForm },
      // 表单验证规则
      rules: {
        username: [
          {
            required: true,
            trigger: 'blur',
            validator: (_rule, value, callback) => {
              if (this.isUpdate) {
                callback();
                return;
              }
              if (!value) {
                return callback(new Error('请输入用户账号'));
              }
              checkExistence('username', value, this.data?.name)
                .then(() => {
                  callback(new Error('账号已经存在'));
                })
                .catch(() => {
                  callback();
                });
            }
          }
        ],
        name: [
          {
            required: true,
            message: '请输入用户名',
            trigger: 'blur'
          }
        ],
        real_name: [
          {
            required: true,
            message: '请输入用户名',
            trigger: 'blur'
          }
        ],

        roleIds: [
          {
            required: true,
            message: '请选择角色',
            trigger: 'blur'
          }
        ],

        password: [
          {
            required: true,
            pattern: /^[\S]{5,18}$/,
            message: '密码必须为6-18位非空白字符',
            trigger: 'blur'
          }
        ],
        phone: [
          {
            pattern: phoneReg,
            message: '手机号格式不正确',
            trigger: 'blur'
          }
        ]
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false
    };
  },
  computed: {
    // 是否开启响应式布局
    styleResponsive () {
      return this.$store.state.theme.styleResponsive;
    }
  },
  methods: {
    radioSelect (e) {
      console.log(e)
      this.radio = e

    },
    async search () {
      this.clickSearch = true
      if (this.form.name) {
        const res = await pageUsers({
          name: this.form.name
        })
        this.radio = 0
        console.log(res)
        this.userInfo = res.list
      }
    },
    /* 保存编辑 */
    async save () {
      if (this.form.name && this.clickSearch) {
        this.loading = true;

        const res = await setTopUser({
          id: this.form.id,
          top_id: this.userInfo[this.radio].id
        })
        if (res.code == 0) {
          this.loading = false;
          this.updateVisible(false)

          this.$message.success(res.message);
          this.$emit('done', {});
        }
      } else {
        this.$message.warning('请选择或者勾选需要修改的上级');
      }
    },
    /* 更新visible */
    updateVisible (value) {
      this.clickSearch = false
      this.userInfo = null
      this.$emit('update:visible', value);
    }
  },
  watch: {
    visible (visible) {
      if (visible) {
        if (this.data) {

          this.$util.assignObject(this.form, {
            ...this.data,

          });
          console.log(this.data)

          this.isUpdate = true;
        } else {
          this.isUpdate = false;
        }
      } else {
        this.$refs.form.clearValidate();
        this.form = { ...this.defaultForm };
      }
    }
  }
};
</script>
<style scoped >
.image {
  width: 44px;
  height: 44px;
  border-radius: 50%;
}
</style>
